package drive.RemakeService.ServiceFormat;

import com.google.gson.Gson;
import drive.Mapper.RoadDisasterMapper;
import drive.Model.RoadDisaster;
import drive.RemakeService.ServiceInterface.RoadHazardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoadHazardServiceA implements RoadHazardService {
    @Autowired
    RoadDisasterMapper roadDisasterMapper;

    Gson gson = new Gson();
    /**
     * 发布灾害
     * @paramdisasterRequest灾害请求对象
     * @return 操作结果的JSON字符串
     */
    @Override
    @Transactional
    public String publishDisaster(RoadDisaster roadDisaster) {
        // 创建灾害记录
        //访问YOLO后端获得灾害类型
//        roadDisaster.setDisasterType("洪水");
        //
        roadDisaster.setStatus(RoadDisaster.Status.unverified);
        roadDisasterMapper.add(roadDisaster);

        return gson.toJson("{"
                + "\"status\":\"success\","
                + "\"message\":\"灾害发布成功\","
                + "\"disasterID\":" + roadDisaster.getDisasterID()
                + "}");
    }

    /**
     * 修改灾害状态
     * @paramdisasterUpdateRequest灾害状态更新请求对象
     * @return 操作结果的JSON字符串
     */
    @Transactional
    @Override
    public String updateDisasterStatus(int disasterID,String status) {
        RoadDisaster roadDisaster = roadDisasterMapper.findByDisasterID(disasterID);
        if (roadDisaster == null) {
            return gson.toJson("{\"status\":\"error\",\"message\":\"灾害记录不存在\"}");
        }

        roadDisaster.setStatus(RoadDisaster.Status.valueOf(status));
        roadDisasterMapper.update(roadDisaster);

        return gson.toJson("{\"status\":\"success\",\"message\":\"灾害状态更新成功\"}");
    }


}